package com.allen.lucas.javawebdemo

import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import com.github.lzyzsd.jsbridge.BridgeWebViewClient
import kotlinx.android.synthetic.main.activity_main.*

/**
 *
 * Create by lal on 2020-09-30
 */
class SecondWebViewActivity:AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        web_view.apply {
            settings.apply {
                javaScriptEnabled = true
                javaScriptCanOpenWindowsAutomatically = true
                domStorageEnabled = true
                displayZoomControls = false
                useWideViewPort = true
                loadWithOverviewMode = true
                layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
                userAgentString += " APP/communityapp"
                cacheMode = WebSettings.LOAD_NO_CACHE
                setAppCacheEnabled(false)

                databaseEnabled = true
                setGeolocationEnabled(true)
                val string = context.getDir("database", Context.MODE_PRIVATE).path
                setGeolocationDatabasePath(string)
            }
            webViewClient = object : BridgeWebViewClient(this) {
                override fun shouldOverrideUrlLoading(
                        view: WebView?,
                        request: WebResourceRequest?
                ): Boolean {
                    Log.e("SecondWebViewActivity", "loadUrl:${request?.url}")
                    return super.shouldOverrideUrlLoading(view, request)
                }

                override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                    Log.e("SecondWebViewActivity", "loadurl2:$url")
                    return super.shouldOverrideUrlLoading(view, url)
                }
            }

            loadUrl("http://192.168.0.138:8080/home")
        }
    }
}